package com.controller;
import javax.swing.JOptionPane;
import com.gui.sup.SupFrame;
import com.tool.DatabaseTool;
public class LoginCon {
	/**
	 *	用户注册
	 */
	public static void doIn(String user,String pass)
	{
		String sql="insert into reader(r_name,r_pass,r_mistake) values(\""+user+"\",\""+pass+"\",0)";
		String validation = "select * from reader where r_name='"+user+"'";
		if(DatabaseTool.doQuery(validation).isEmpty()){
			DatabaseTool.doInsert(sql);
			JOptionPane.showMessageDialog(null, "注册成功", "提示",JOptionPane.INFORMATION_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(null, "用户名已经被注册了！", "警告",JOptionPane.WARNING_MESSAGE); 	
		}
	}
	/**
	 *	管理员注册
	 */
	public static void adminIn(String user,String pass)
	{
		String sql="insert into admin(a_name,a_pass) values(\""+user+"\",\""+pass+"\")";
		String validation = "select * from admin where a_name='"+user+"'";
		if(DatabaseTool.doQuery(validation).isEmpty()){
			DatabaseTool.doInsert(sql);
			JOptionPane.showMessageDialog(null, "注册成功", "提示",JOptionPane.INFORMATION_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(null, "用户名已经被注册了！", "警告",JOptionPane.WARNING_MESSAGE); 	
		}
	}
	/**
	 * 	读者登录
	 */
	public static boolean userSel(String user,String pass)
	{
		String sql="select r_name,r_pass from reader where r_name='"+user+"'and r_pass='"+pass+"'";
		if(DatabaseTool.doQuery(sql).isEmpty())
		{
			JOptionPane.showMessageDialog(null, "用户名密码错误", "警告",JOptionPane.WARNING_MESSAGE); 
			return false;
		}
		return true;
	}
	/**
	 *	管理员登录
	 */
	public static boolean adminSel(String user,String pass)
	{
		String sql="select a_name,a_pass from admin where a_name='"+user+"'and a_pass='"+pass+"'";
		if(DatabaseTool.doQuery(sql).isEmpty())
		{
			JOptionPane.showMessageDialog(null, "用户名密码错误", "警告",JOptionPane.WARNING_MESSAGE);
			return false;
		}
		
		if(user.equals("admin") && pass.equals("admin"))
		{
			new SupFrame();
			return false;
		}
		return true;
	}
}
